<template>
  <Select clearable filterable v-model="belongOrgName" placeholder="请选择企业"
          :remote-method="remoteMethodSelectOrg"
          :default-label="defaultLabel"
          :loading="orgLoading">
    <Option v-for="(option, index) in orgLists" :value="option.orgName" :key="index">{{ option.orgName }}</Option>
  </Select>
</template>

<script>
import {Api} from "@/api";
import {sessionStorage} from "@/common/cacheUtils";

export default {
  name: "companySelect",
  props: {
    value: {
      type: String,
    },
    root: {
      type: Boolean,
      defaultValue: false,
    }
  },
  data() {
    return {
      belongOrgName:"",
      defaultLabel:sessionStorage('userId').getObject('userId').companyName,
      orgLists:[],
      userInfo: sessionStorage('userId').getObject('userId'),
      orgLoading:false,
    }
  },
  created() {
  },
  methods: {
    onChange(data) {
      this.$emit("on-change", data)
    },
    remoteMethodSelectOrg (query) {
      if (query !== '') {
        this.orgLists = [];
        this.orgLoading = true;
        let url = Api.queryOrgList + "?companyId=" + this.userInfo.companyId + "&orgName=" + query + "&fromPage=1&pageSize=50"
        if (this.root) {
          url = Api.queryOrgList + "?companyId=18000000&orgName=" + query + "&fromPage=1&pageSize=50"
        }
        this.$http.get(url).then(res => {
          this.orgLists = res.data.data;
          this.orgLoading = false;
        }).catch(e => {
          this.orgLoading = false;
          this.$Message.error('请求失败');
        })
      } else {
        this.orgLoading = false;
      }
    },
  },
  watch: {
    value: {
      immediate: true,
      handler(n) {
        this.belongOrgName = n;
      },
    },
    belongOrgName(n, o) {
      this.$emit("input", n)
    }
  },
}
</script>

<style scoped>

</style>
